package com.redtree.air.db
{
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.rpc.events.ResultEvent;

	public class Depts extends Model
	{
		public function Depts()
		{
			super();
			tableName = getTableName('depts');
		}
		public function getDepts(underSecurity:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var sql:String = "select * from depts";
			if( underSecurity )
				sql += " where security_level "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			if(Model.instance.query(sql))var result:Array = Model.instance.data;
			var resultAry:Array = new Array();
			for( var i:int=0;i<result.length; i++ )
				resultAry.push(new Dept(result[i]));
			return resultAry;
		}
		/**
		 * 
		 * @param underSecurity
		 * @param justCurrentLevel
		 * @return Array
		 * 	可看到的dept
		 * 
		 */
		public function getDeptsAsArray(underSecurity:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var sql:String = "select name as label,id as data from depts";
			if( underSecurity )
				sql += " where security_level "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			var result:Array;
			if(Model.instance.query(sql))result = (Model.instance.data );
			return result;
		}
		/**
		 * 删除
		 */
		override public function deleteElement( id:int ):Boolean
		{
			var dept:Dept = this.getDeptById(id);
			if( dept.isSysOp() ) return false;
			var sql:String = "delete from "+tableName+" where id = "+id;
			return Model.instance.query(sql);
		}
		/**
		 * 得到一个部门
		 */
		public function getDeptById( id:int ):Dept
		{
			var sql:String = "select * from "+tableName+" where id = "+id;
			if(Model.instance.query(sql))var result:Array = Model.instance.data;
			var resultAry:Array = new Array();
			for( var i:int=0;i<result.length; i++ )
				resultAry.push(new Dept(result[i]));
			return resultAry[0];
		}

	}
}